package services;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DataAccess.DeviceDA;

import pojo.DevicePOJO;

/**
 * Servlet implementation class ListDevice
 */
@WebServlet("/ListDevice")
public class ListDevice extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ListDevice() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		out.print("<html><body>");
		out.print("<head>");
		out.print("  <title>GCM Demo</title>");
		out.print("  <link rel='icon' href='favicon.png'/>");
		out.print("</head>");
		out.print("<body>");
		out.print("<h1>Danh sách devices 2:</h1>");
		DeviceDA da = new DeviceDA();
		ArrayList<DevicePOJO> listdv = da.get();
		if (listdv != null) {
			int i = 0;
			for (DevicePOJO dv : listdv) {
				out.print(i + "." + dv.getRegID() + "<br>");
				i++;
			}
		}
		out.print("<h2>" + listdv.size() + " device(s) registered!</h2>");
		out.print("<form name='form' method='POST' action='SendAll'>");
		out.print("<input type='submit' value='Send Message' />");
		out.print("</form>");
		out.print("</body></html>");
		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		// TODO Auto-generated method stub
	}

}
